﻿Imports System.Net.Mail
Imports System.IO
Imports System
Imports System.Xml
Imports System.Web.UI.Page
Imports Microsoft.VisualBasic

Public Class MetodosComuns

  Public Const AVATAR_WIDTH As Integer = 150
  Public Const AVATAR_HEIGHT As Integer = 150

  Public Const THUMBNAIL_WIDTH As Integer = 160
  Public Const THUMBNAIL_HEIGHT As Integer = 120
  Public Const VIDEO_WIDTH As Integer = 420
  Public Const VIDEO_HEIGHT As Integer = 315

  Public Const constTAMANHOMAXTEXTO As Integer = 8000
  Public Const DIRETORIOIMAGENSFITAS As String = "../imagens/"
  Public Const DIRETORIOIMAGENSPROMOCAO As String = "../imagens/promocoes/"
  Public Const STREMAILFIT4 As String = "extranet@reebokfitness.com.br"
  Public Const STREMAILSENHA As String = "extranet123"
  Public Const STRENDSERVIDORSMTP As String = "smtp.reebokfitness.com.br"
  Public Const PORTASERVIDORSMTP As Integer = 587
  Public Const SMTP As String = "smtp.reebokfitness.com.br"
  Public Shared STRMENSAGEMESQUECISENHA As String = "Você solicitou lembrete de senha para a Extranet Fit4.</br>Seu usuário é: strUsuario</br>Senha: strSenha"
  Public Shared STREMAILADMINISTRADOR As String = "allan.roosch@fit4.com.br"

  Public Shared Function GetCurrentPageName() As String
    Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
    Dim oInfo As System.IO.FileInfo = New System.IO.FileInfo(sPath)
    Return oInfo.Name
  End Function

  Public Shared Function GetVirtualPath() As String
    Dim caminho As String
    caminho = If(HttpRuntime.AppDomainAppVirtualPath.EndsWith("/"), HttpRuntime.AppDomainAppVirtualPath.Replace("/", ""), HttpRuntime.AppDomainAppVirtualPath)
    Return caminho
  End Function

  Public Shared Function ConverteTextoParaDecimal(ByVal strTexto As String) As Decimal
    Dim decimalTeste As String = "10,000"
    Dim dec As Decimal = 0
    'Testa para ver se a formatacao de numeros eh ingles ou portugues
    If Convert.ToDecimal(decimalTeste) = 10000 Then
      ' eh ingles
      dec = Convert.ToDecimal(strTexto)
    ElseIf Convert.ToDecimal(decimalTeste) = 10 Then
      'eh portugues
      dec = Convert.ToDecimal(strTexto.Replace(".", ","))
    End If
    Return dec
  End Function

  Public Shared Function DescobreIdiomaAtual() As String
    Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
    Dim oInfo As System.IO.FileInfo = New System.IO.FileInfo(sPath)
    Dim sRet As String

    Select Case oInfo.Directory.Name
      Case "english"
        sRet = "inglês"
      Case Else
        sRet = "português"
    End Select

    Return sRet
  End Function

  Public Shared Function removeAcentos(ByVal strTexto As String) As String
    Dim i As Integer
    Dim C_acentos As String = "áéíóúÁÉÍÓÚâêîôûÂÊÎÔÛãõÃÕçÇäëïöüÄËÏÖÜàèìòùÀÈÌÒÙ!#$%¨&*@"
    Dim S_acentos As String = "aeiouAEIOUaeiouAEIOUaoAOcCaeiouAEIOUaeiouAEIOU_______A"
    For i = 0 To C_acentos.Length - 1
      strTexto = strTexto.Replace(C_acentos(i), S_acentos(i).ToString())
    Next
    Return strTexto
  End Function

  Public Shared Sub link_idioma(ByRef tela As System.Web.UI.Page)
    Dim hlink As HyperLink

    hlink = tela.Master.FindControl("Portugues")
    hlink.NavigateUrl = ".." & "/portugues/" & GetCurrentPageName()

    hlink = tela.Master.FindControl("English")
    hlink.NavigateUrl = ".." & "/english/" & GetCurrentPageName()
  End Sub

  Public Shared Sub EnviaEmail(ByVal strEnderecoEmail As String, _
                               ByVal strCorpoEmail As String, _
                               ByVal strAssunto As String, _
                               ByVal strEmailRemetente As String, _
                               ByVal strNomeRemetente As String)
    Dim objMensagemEmail As New MailMessage
    Dim intCont As Integer = 0
    Dim vetorEnderecos As String()
    Dim objendereco As MailAddress
    Dim objClienteSmtp As SmtpClient

    objClienteSmtp = New SmtpClient(STRENDSERVIDORSMTP, PORTASERVIDORSMTP)
    vetorEnderecos = strEnderecoEmail.Split(";") 'caso existam varios emails separados por ;
    objendereco = New MailAddress(strEmailRemetente, STREMAILFIT4)
    objClienteSmtp.Credentials = New Net.NetworkCredential(STREMAILFIT4, STREMAILSENHA)

    For intCont = 0 To UBound(vetorEnderecos)
      objMensagemEmail.To.Add(vetorEnderecos(intCont))
    Next
    objMensagemEmail.IsBodyHtml = True
    objMensagemEmail.Body = strCorpoEmail
    objMensagemEmail.Sender = objendereco
    objMensagemEmail.From = objendereco
    objMensagemEmail.Priority = MailPriority.Normal
    objMensagemEmail.Subject = strAssunto
    objClienteSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
    objMensagemEmail.BodyEncoding = System.Text.Encoding.Default
    objMensagemEmail.SubjectEncoding = System.Text.Encoding.Default
    strEnderecoEmail = strEnderecoEmail.Replace("/", "")
    strEnderecoEmail = strEnderecoEmail.Replace(vbLf, "")
    strEnderecoEmail = strEnderecoEmail.Replace(" ", "")
    Try
      objClienteSmtp.Send(objMensagemEmail)
    Catch ex As Exception
      Dim exceptionEmail As New Exception("Erro no envio do email.")
      Throw exceptionEmail
    End Try
  End Sub

  Public Shared Sub substitui_Aspas_Simples(ByRef strCampo As String)
    strCampo = strCampo.Replace("'", "''")
  End Sub

  'Tela para Logout
  Public Const TELA_LOGOUT As String = "~/Logout.aspx"

  'Estados da Tela de Interface
  Public Const ESTADO_BLOQUEADO As Int16 = 0
  Public Const ESTADO_INICIAL As Int16 = 1
  Public Const ESTADO_BUSCA As Int16 = 2
  Public Const ESTADO_LISTA As Int16 = 3
  Public Const ESTADO_DETALHE As Int16 = 4
  Public Const ESTADO_EDICAO As Int16 = 5
  Public Const ESTADO_CADASTRO As Int16 = 6
  Public Const ESTADO_EXCLUSAO As Int16 = 7

  Shared Function LocalizaStringsNoTexto(ByVal sStrings As String, ByVal sTexto As String) As Boolean
    Dim aTexto As New ArrayList, sText As String
    Dim aStrings As Array = removeAcentos(sStrings).ToUpper().Split(" "), nI As Integer
    sTexto = removeAcentos(sTexto).ToUpper()
    For nI = 0 To aStrings.Length - 1
      sText = aStrings(nI).ToString.Trim()
      If sText.Length > 0 Then aTexto.Add(sText)
    Next
    For nI = 0 To aTexto.Count - 1
      If (sTexto.Contains(aTexto(nI))) Then Return True
    Next
    Return aTexto.Count = 0
  End Function

  Shared Sub CalculaDimensionesProporcionais(ByRef nWidthOriginal As Integer, ByRef nHeightOriginal As Integer, _
                                             ByVal nWidthRef As Integer, ByVal nHeightRef As Integer)
    If nWidthOriginal <= 0 OrElse nHeightOriginal <= 0 OrElse nWidthRef <= 0 OrElse nHeightRef <= 0 Then Return
    If nWidthOriginal / nWidthRef > nHeightOriginal / nHeightRef Then
      nHeightOriginal *= nWidthRef / nWidthOriginal
      nWidthOriginal *= nWidthRef / nWidthOriginal
    Else
      nWidthOriginal *= nHeightRef / nHeightOriginal
      nHeightOriginal *= nHeightRef / nHeightOriginal
    End If
  End Sub

  Shared Sub GerarXmlVideo(ByVal objBEVideo As extranet.BE.BEVideo)
    Try
      Dim writer As New XmlTextWriter(objBEVideo.Diretorio & "\xml\" & objBEVideo.Video & ".xml", New System.Text.UTF8Encoding())
      'inicia o documento xml
      writer.WriteStartDocument()
      'define a indentação do arquivo
      writer.Formatting = Formatting.Indented
      'escreve o elmento raiz
      writer.WriteStartElement("config")

      'Inicia um elemento
      writer.WriteStartElement("param")
      'Inclui um atributo ao elemento
      writer.WriteAttributeString("name", "flv")
      writer.WriteAttributeString("value", "../video/" & objBEVideo.Video)
      ' encerra o elemento 
      writer.WriteEndElement()

      Dim intWidth As Integer = objBEVideo.Width, intHeight As Integer = objBEVideo.Height
      CalculaDimensionesProporcionais(intWidth, intHeight, VIDEO_WIDTH, VIDEO_HEIGHT)
      'escreve um segundo elemento usando um raw de dados strings
      'Chr(10) & " <param name=""width"" value=""" & intWidth & """ />" & _
      'Chr(10) & " <param name=""height"" value=""" & intHeight & """ />" & _
      writer.WriteRaw(Chr(10) & " <param name=""autoplay"" value=""0"" />" & _
                      Chr(10) & " <param name=""autoload"" value=""0"" />" & _
                      Chr(10) & " <param name=""buffer"" value=""5"" />" & _
                      Chr(10) & " <param name=""buffermessage"" value="""" />" & _
                      Chr(10) & " <param name=""buffercolor"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""bufferbgcolor"" value=""376f53"" />" & _
                      Chr(10) & " <param name=""buffershowbg"" value=""1"" />" & _
                      Chr(10) & " <param name=""title"" value=""" & objBEVideo.Titulo.Replace("""", "'") & """ />" & _
                      Chr(10) & " <param name=""titlesize"" value=""15"" />" & _
                      Chr(10) & " <param name=""titlecolor"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""margin"" value=""0"" />" & _
                      Chr(10) & " <param name=""srt"" value=""0"" />" & _
                      Chr(10) & " <param name=""srtcolor"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""srtbgcolor"" value=""376f53"" />" & _
                      Chr(10) & " <param name=""srtsize"" value=""11"" />" & _
                      Chr(10) & " <param name=""showstop"" value=""1"" />" & _
                      Chr(10) & " <param name=""showvolume"" value=""1"" />" & _
                      Chr(10) & " <param name=""showtime"" value=""1"" />" & _
                      Chr(10) & " <param name=""showplayer"" value=""autohide"" />" & _
                      Chr(10) & " <param name=""showloading"" value=""autohide"" />" & _
                      Chr(10) & " <param name=""showfullscreen"" value=""1"" />" & _
                      Chr(10) & " <param name=""showswitchsubtitles"" value=""0"" />" & _
                      Chr(10) & " <param name=""showmouse"" value=""autohide"" />" & _
                      Chr(10) & " <param name=""loop"" value=""0"" />" & _
                      Chr(10) & " <param name=""startimage"" value=""startImage/" & objBEVideo.Video.ToString() & ".jpg"" />" & _
                      Chr(10) & " <param name=""playercolor"" value=""376f53"" />" & _
                      Chr(10) & " <param name=""loadingcolor"" value=""bbbbbb"" />" & _
                      Chr(10) & " <param name=""bgcolor"" value=""376f53"" />" & _
                      Chr(10) & " <param name=""bgcolor1"" value=""4b7d64"" />" & _
                      Chr(10) & " <param name=""bgcolor2"" value=""376f53"" />" & _
                      Chr(10) & " <param name=""buttoncolor"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""buttonovercolor"" value=""adbfb6"" />" & _
                      Chr(10) & " <param name=""slidercolor1"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""slidercolor2"" value=""bbbbbb"" />" & _
                      Chr(10) & " <param name=""sliderovercolor"" value=""adbfb6"" />" & _
                      Chr(10) & " <param name=""loadonstop"" value=""0"" />" & _
                      Chr(10) & " <param name=""onclick"" value=""playpause"" />" & _
                      Chr(10) & " <param name=""onclicktarget"" value=""_blank"" />" & _
                      Chr(10) & " <param name=""ondoubleclick"" value=""fullscreen"" />" & _
                      Chr(10) & " <param name=""ondoubleclicktarget"" value=""_blank"" />" & _
                      Chr(10) & " <param name=""playertimeout"" value=""1500"" />" & _
                      Chr(10) & " <param name=""videobgcolor"" value=""000000"" />" & _
                      Chr(10) & " <param name=""volume"" value=""100"" />" & _
                      Chr(10) & " <param name=""shortcut"" value=""1"" />" & _
                      Chr(10) & " <param name=""playeralpha"" value=""100"" />" & _
                      Chr(10) & " <param name=""phpstream"" value=""0"" />" & _
                      Chr(10) & " <param name=""srturl"" value=""video.srt"" />" & _
                      Chr(10) & " <param name=""top1"" value=""0"" />" & _
                      Chr(10) & " <param name=""showiconplay"" value=""0"" />" & _
                      Chr(10) & " <param name=""iconplaycolor"" value=""ffffff"" />" & _
                      Chr(10) & " <param name=""iconplaybgcolor"" value=""dddddd"" />" & _
                      Chr(10) & " <param name=""iconplaybgalpha"" value=""25"" />" & _
                      Chr(10) & " <param name=""showtitleandstartimage"" value=""1"" />" & _
                      Chr(10) & "")

      ' encerra o elemento raiz
      writer.WriteEndElement()

      'Escreve o XML para o arquivo e fecha o objeto escritor 
      writer.Close()
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.GeraXmlVideo(): " & ex.Message)
    End Try

  End Sub

End Class
